#include <string>
#include <iostream>

using namespace std;

// 抽象处理者
class Handler {
public:
    virtual ~Handler() {
        delete chain;
    };

protected:
    Handler *chain = nullptr;

public:
    virtual void setNext(Handler *handler) {
        chain = handler;
    }

    virtual void handle(int request) {
        if (chain != nullptr) {
            chain->handle(request);
        }
    }
};

// 具体处理者A
class ConcreteHandlerA : public Handler {
public:
    void handle(int request) override {
        if (request < 10) {
            cout << "ConcreteHandler A Manage Current Request : " << request << endl;
        }

        Handler::handle(request);
    }
};

// 具体处理者B
class ConcreteHandlerB : public Handler {
public:
    void handle(int request) override {
        if (request <= 20) {
            cout << "ConcreteHandler B Handle Current Request : " << request << endl;
            return;
        }

        Handler::handle(request);
    }
};

// 具体处理者C
class ConcreteHandlerC : public Handler {
public:
    void handle(int request) override {
        if (request >= 20) {
            cout << "ConcreteHandler C Handle Current Request : " << request << endl;
        }

        Handler::handle(request);
    }
};

void test01() {
    Handler *handlerA = new ConcreteHandlerA();
    Handler *handlerB = new ConcreteHandlerB();
    Handler *handlerC = new ConcreteHandlerC();

    handlerA->setNext(handlerB);
    handlerB->setNext(handlerC);

    handlerA->handle(5);
    cout << endl;

    handlerA->handle(15);
    cout << endl;

    handlerA->handle(25);

    delete handlerA;
    delete handlerB;
    delete handlerC;
}

int main() {
    test01();

    return 0;
}